package com.hook3.pianke.activity.base.radiouserbasepager.RadioService;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.hook3.pianke.domain.PrivateRadioInfo;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by HOME on 2015/12/11.
 */
public class MusicRadioService extends Service {
    public ArrayList<PrivateRadioInfo.PrivateRadioEachContent> privateRadiolist;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle bundle = intent.getExtras();
        privateRadiolist = (ArrayList<PrivateRadioInfo.PrivateRadioEachContent>) bundle.getSerializable("privateRadiolist");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {

        return new Middle();
    }


    public class Middle extends Binder{
        MusicRadioService service = MusicRadioService.this;

        public void openAudio(int position) {
            service.openAudio(position);
        }

        public void pause(){
            service.pause();
        }

        public void stop(){
            service.stop();
        }

        public boolean isPlaying(){
           return service.isPlaying();
        }

        public void start(){
            service.start();
        }

        public void seekTo(int progress){
            service.seekTo(progress);
        }

        public int getCurrentProgress() {
           return service.getCurrentProgress();
        }
    }

    MediaPlayer mediaPlayer;

    int spRadioNumber;//上一首歌的id
    int openAudioPosition;
    SharedPreferences sp;
    int progress; //当前播放歌曲的进度
    private void openAudio(int position) {
        sp = getSharedPreferences("config", MODE_PRIVATE);
        openAudioPosition = position;

        spRadioNumber = sp.getInt("CURRENTPOSIATION", -1);

        try {
            if(mediaPlayer != null){
                mediaPlayer.reset();
                //保存到sp中，将最新歌曲id;

            }

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnPreparedListener(mOnPreparedListener);
            mediaPlayer.setDataSource(privateRadiolist.get(position).musicUrl);
            mediaPlayer.prepareAsync();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**播放器准备好
     * 音乐
     */
    private MediaPlayer.OnPreparedListener mOnPreparedListener =
            new MediaPlayer.OnPreparedListener(){

                @Override
                public void onPrepared(MediaPlayer mp) {
                        start();
                    if(openAudioPosition == spRadioNumber){
                        seekTo(progress);
                    }
                    sp.edit().putInt("CURRENTPOSIATION",openAudioPosition).commit();
                }
            };

    /**
     * 开始播放
      */
    private void start() {
        mediaPlayer.start();
    }

    /**
     * 暂停播放
     */
    private void pause() {
        mediaPlayer.pause();
    }

    /**
     * 停止服务
     */
    private void stop() {
        mediaPlayer.stop();
    }

    /**
     *正在播放
     */
    private boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    /**
     * 当前播放进度
     * @param
     */
    private int getCurrentProgress(){
        if(mediaPlayer != null){
            return mediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    /**
     * 获取歌曲的size
     * @param rootIntent
     */
//    private int getDuration(){
//        if(mediaPlayer != null){
//            return
//        }
//    }

    /**
     * 按照进度播放歌曲
     * @param
     */
    private void seekTo(int progress){
        mediaPlayer.seekTo(progress);
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Intent intent = new Intent(getApplicationContext(),
                MusicRadioService.class);
        stopService(intent);
    }


}
